// 实习状态管理
import { defineStore } from 'pinia'
import apiClient from '../api'

export const useInternshipStore = defineStore('internship', {
    state: () => ({
        currentInternship: null,
        internshipDetails: null,
        materials: []
    }),

    actions: {
        // 获取实习详情
        async fetchInternship(id) {
            try {
                const response = await apiClient.get(`/internships/${id}`)
                this.internshipDetails = response.data
                return response.data
            } catch (error) {
                throw new Error(error.response?.data?.message || '获取实习详情失败')
            }
        },

        // 分配指导教师
        async assignTeacher(internshipId, teacherId) {
            try {
                const response = await apiClient.put(`/internships/${internshipId}/assign-teacher`, {
                    teacherId
                })
                this.internshipDetails = response.data
                return response.data
            } catch (error) {
                throw new Error(error.response?.data?.message || '分配指导教师失败')
            }
        },

        // 填写企业导师信息
        async updateMentorInfo(internshipId, mentorData) {
            try {
                const response = await apiClient.put(`/internships/${internshipId}/mentor-info`, mentorData)
                this.internshipDetails = response.data
                return response.data
            } catch (error) {
                throw new Error(error.response?.data?.message || '更新导师信息失败')
            }
        },

        // 更新实习状态
        async updateStatus(internshipId, status) {
            try {
                const response = await apiClient.put(`/internships/${internshipId}/status`, { status })
                this.internshipDetails = response.data
                return response.data
            } catch (error) {
                throw new Error(error.response?.data?.message || '更新实习状态失败')
            }
        },

        // 获取实习材料
        async fetchMaterials(internshipId) {
            try {
                const response = await apiClient.get(`/internship-materials/internship/${internshipId}`)
                this.materials = response.data
                return response.data
            } catch (error) {
                throw new Error(error.response?.data?.message || '获取实习材料失败')
            }
        }
    }
})
